<HTML>
<!--
	THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
								J. Brook Monroe, mrprogguy@techie.com
    Copyright (c)1998 by Charles River Media.  All Rights Reserved.
    
    This applet can only be re-used or modifed by license holders of the
    JavaScript Cookbook CD-ROM.  Credit must be given in the source
    code and this copyright notice must be maintained. If you do
    not hold a license to the JavaScript Cookbook, you may NOT
    duplicate or modify this code for your own use.

	Use at your own risk. No warranty is given or implied of the suitability 
	of this applet for any specific application. Neither Erica Sadun,
	J. Brook Monroe nor Charles River Media will be held responsible for any
	unwanted effects due to the use of this applet or any derivative. 
-->	
<HEAD>
	<TITLE>Reversing an Array</TITLE>
</HEAD>
<SCRIPT LANGUAGE="JavaScript1.2"><!--
var myArray;

function Go()
{
	var i;
	for(i = 0; i < 8; i++)
		myArray[i] = eval("document.forms[0].slot"+i+".value");
	myArray.reverse();
	for(i = 0; i < 8; i++)
		eval("document.forms[0].out"+i+".value='"+myArray[i]+"'");
}

function Warning(cellId)
{
	eval("document.forms[1].out"+cellId+".value=''");
	alert("No, you shouldn't type in that cell!");
}
myArray = new Array(8);
//-->
</SCRIPT>
<BODY>
<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50
ALIGN = LEFT>Reversing an Array</H1></FONT>
<BLOCKQUOTE><FONT COLOR="770000">
In this recipe we find out that JavaScript in Navigator can reverse arrays for us.
</FONT><P>
Fill in the top row of boxes and press the "GO!" button.<BR>
<FORM>
Original:<BR>
<INPUT TYPE="text" VALUE="" NAME="slot0" SIZE=8>
<INPUT TYPE="text" VALUE="" NAME="slot1" SIZE=8>
<INPUT TYPE="text" VALUE="" NAME="slot2" SIZE=8>
<INPUT TYPE="text" VALUE="" NAME="slot3" SIZE=8>
<INPUT TYPE="text" VALUE="" NAME="slot4" SIZE=8>
<INPUT TYPE="text" VALUE="" NAME="slot5" SIZE=8>
<INPUT TYPE="text" VALUE="" NAME="slot6" SIZE=8>
<INPUT TYPE="text" VALUE="" NAME="slot7" SIZE=8>
<INPUT TYPE="button" VALUE="GO!" onClick="Go()"><BR>
Reversed:<BR>
<INPUT TYPE="text" VALUE="" NAME="out0" SIZE=8 onChange="Warning(0)">
<INPUT TYPE="text" VALUE="" NAME="out1" SIZE=8 onChange="Warning(1)">
<INPUT TYPE="text" VALUE="" NAME="out2" SIZE=8 onChange="Warning(2)">
<INPUT TYPE="text" VALUE="" NAME="out3" SIZE=8 onChange="Warning(3)">
<INPUT TYPE="text" VALUE="" NAME="out4" SIZE=8 onChange="Warning(4)">
<INPUT TYPE="text" VALUE="" NAME="out5" SIZE=8 onChange="Warning(5)">
<INPUT TYPE="text" VALUE="" NAME="out6" SIZE=8 onChange="Warning(6)">
<INPUT TYPE="text" VALUE="" NAME="out7" SIZE=8 onChange="Warning(7)">
</FORM>
<FONT COLOR="007777"><H2>Discussion</H2></FONT>
<FONT SIZE=4>
Everything you need to know about this capability can be summed up in two code fragments.  Which would you rather write?
<H3><FONT COLOR="007777">The Hard Way</FONT></H3>
<FONT COLOR="770000"><PRE>
function Reverse(anArray)
{
	var len	= anArray.length;
	var tempArray = new Array(len); 
	var i;
	var j = len - 1 ;
	for(i = 0; i < len; i++) {
		tempArray[i] = anArray[j];
		j -= 1;
	}
	return tempArray;	
}
</PRE></FONT>
<H3><FONT COLOR="007777">The Easy Way</FONT></H3>
<FONT COLOR="770000"><PRE>anArray.reverse();</PRE></FONT>
Perhaps we've tried to sway you by calling one the hard way and one the easy way, but we think we'll get little argument
that letting the JavaScript engine do it is the preferred method.
</FONT>
<BR><BR><h5>Copyright ©1998 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>